import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtTeamApplyApi } from '#/api/cmt/team/apply';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getTeamInfoOptions } from '#/api/cmt/team/info';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'teamId',
      label: '团队ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择团队',
        api: getTeamInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'personId',
      label: '人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'applyMessage',
      label: '申请留言',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入申请留言',
      },
    },
    {
      fieldName: 'applyStatus',
      label: '申请状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.CMT_TEAM_APPLY_STATUS, 'number'),
        placeholder: '请选择申请状态',
      },
    },
    {
      fieldName: 'auditUserId',
      label: '审核人ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入审核人ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditTime',
      label: '审核时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择审核时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入审核备注',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'teamId',
      label: '团队ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择团队',
        api: getTeamInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'personId',
      label: '人员ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'applyStatus',
      label: '申请状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.CMT_TEAM_APPLY_STATUS, 'number'),
        placeholder: '请选择申请状态',
      },
    },
    {
      fieldName: 'auditUserId',
      label: '审核人ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入审核人ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtTeamApplyApi.TeamApply>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 120,
    },
    {
      field: 'teamId',
      title: '团队ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '会员ID',
      minWidth: 120,
    },
    {
      field: 'personId',
      title: '人员ID',
      minWidth: 120,
    },
    {
      field: 'applyMessage',
      title: '申请留言',
      minWidth: 200,
    },
    {
      field: 'applyStatus',
      title: '申请状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_TEAM_APPLY_STATUS },
      },
    },
    {
      field: 'auditUserId',
      title: '审核人ID',
      minWidth: 120,
    },
    {
      field: 'auditTime',
      title: '审核时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'auditRemark',
      title: '审核备注',
      minWidth: 200,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
